package com.jasmine.auth.util;

import com.jasmine.auth.TokenContextHolder;
import com.jasmine.auth.model.Token;
import com.jasmine.auth.model.UserInfo;

import java.util.Optional;

/**
 * @author jasmineXz
 */
public class AuthUtil {

    public static Token getTokenContext () {
        return TokenContextHolder.getContext();
    }

    public static Integer getUserId () {
        Integer userId = getUserInfo().getUserId();
        Optional.ofNullable(userId).orElseThrow(NullPointerException::new);
        return userId;
    }

    public static String getUsername () {
        String username = getUserInfo().getUsername();
        Optional.ofNullable(username).orElseThrow(NullPointerException::new);
        return username;
    }

    private static UserInfo getUserInfo(){
        return getTokenContext().getUserInfo();
    }
}
